﻿using Yd.Domain.Abstract;
using System.Data;
using System.Data.SqlClient;
using Yd.Domain.IDAL;
using System.Collections.Generic;
using Yd.Model;
using System;

namespace Yd.Domain.DAL
{
    public class CommDAL:BaseCls,ICommDAL
    {

        /// <summary>
        /// 获取菜单信息
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public DataSet GetPurview(string userid, string fparentId="0")
        {
            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@PurviewParentId",fparentId),
                new SqlParameter("@userid",userid)
            };
            return QueryObject("GetPurview", fparam);
        }

        public void SaveLOG(string uid, string username, string log)
        {
            SaveOrUpdateSql("Insert Into tz_log(uid,username,fcontent) values("+uid+",'"+username+"','"+log+"')");
        }

        public void UpdateHit(string Fid)
        {
            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@Id",Fid)
            };
            ExecuteStorePro("UpdateHit", fparam);
        }

        public DataSet GetWebSite()
        {
            return QueryObjectAllSql("select * from yd_webconfig where wid=1");
        }
        public bool g1(int userId)
        {
            DataSet ds = QueryObjectAllSql("select uid from yd_users where disabled=1 and uid=" + userId);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        public bool g1byopenid(string openid)
        {
            DataSet ds = QueryObjectAllSql("select uid from yd_users where disabled=1 and openid=" + openid);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        public bool g2(int shopId)
        {
            DataSet ds = QueryObjectAllSql("select suid from yd_shop where isForbidden=1 and suid=" + shopId);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        public bool g2byopenid(string openid)
        {
            DataSet ds = QueryObjectAllSql("select suid from yd_shop where isForbidden=1 and openid=" + openid);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        public int updatepwd(string id, string pwd)
        {
            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@Id",id),
                new SqlParameter("@PassWord",pwd)
            };
            return ExecuteStorePro("UpdatePwd", fparam);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Fid"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public DataSet GetNewsPre1(string Fid, int state = 0)
        {
            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@Id",Fid),
                new SqlParameter("@state",state)
            };
            return QueryObject("GetNewsPre1", fparam);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataSet GetWebInfo()
        {
            return QueryObject("GetWebInfo", null);
        }

        public DataSet GetNewsTj()
        {
            return QueryObject("GetNewsTj", null);
        }

        /// <summary>
        /// 获取新闻列表
        /// </summary>
        /// <param name="SortId"></param>
        /// <returns></returns>
        public DataSet GetTopNews(string SortId,int NewCount)
        {
            SqlParameter[] fparam = new SqlParameter[] { 
                    new SqlParameter("@BigSortId",SortId),
                    new SqlParameter("@NewCount",NewCount)
            };
            return QueryObject("GetTopNews", fparam);
        }


        /// <summary>
        /// 获取活动信息
        /// </summary>
        /// <param name="Fid"></param>
        /// <returns></returns>
        public DataSet Gettz_news(string Fid)
        {

            return QueryObjectAllSql("select * from yd_action where hdId=" + Fid);

        }
        public DataSet Gettz_tick(string Fid)
        {

            return QueryObjectAllSql("select [tid],[hdId],[ticketName],[fare],(SELECT CONVERT(VARCHAR(100), endTime, 20)) as endTime,[allVotes] from yd_ticket where endTime>getdate() and hdId=" + Fid);

        }
        public int UpdateAction(yd_action action)
        {
            return SaveOrUpdateSql("Update yd_action set actionName='" + action.actionName + "',actionContent='" + action.actionContent + "',actionAddress='" + action.actionAddress + "',startTime='" + action.startTime + "',endTime='" + action.endTime + "',actionCost=" + action.actionCost + ",actionImgUrl='" + action.actionImgUrl + "',ticketInformation='" + action.ticketInformation + "' where hdId=" + action.hdId);
        }
        public int InstallAction(yd_action action)
        {
            //return SaveOrUpdateSql("Insert Into yd_action(actionType,shopid,actionName,actionImgUrl,actionContent,actionAddress,actionCost,startTime,endTime) values("+action.actionType+","+action.shopid+",'" + action.actionName + "','" + action.actionImgUrl + "','" + action.actionContent + "','" + action.actionAddress + "',"+action.actionCost+",'" + action.startTime + "','" + action.endTime + "')");
            return base.InsertObject(action, 10, "InsertAction");
        }
        /// <summary>
        /// 获取类别
        /// </summary>
        /// <param name="fparentId"></param>
        /// <returns></returns>
        public DataSet GetSort(string fparentId="0")
        {
            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@SortParentId",fparentId)
            };
            return QueryObject("QueryHr_Sort", fparam);
        }

        public DataSet QuerySort(string fparentId = "0")
        {
            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@SortParentId",fparentId)
            };
            return QueryObject("QueryHr_Sort_1", fparam);
        }

        public DataSet GetLj()
        {
            return QueryObjectAllSql("select * from tz_yqlj order by Id desc");
        }

        #region 注册用户
        public int InsertUser(yd_users tzuser)
        {
            return base.InsertObject(tzuser, 2, "LoginOrRegUser");
        }
        #endregion

        #region 注册商家
        public int InsertShop(yd_shop tzuser)
        {
            return base.InsertObject(tzuser, 2, "LoginOrRegShop");
        }
        #endregion

        #region 添加订单
        public int InsertOrder(yd_order tzuser)
        {
            return base.InsertObject(tzuser, 5, "InsertOrder");
        }
        #endregion

        #region 添加卡座
        public int InsertKazuo(yd_kazuo tzuser)
        {
            return base.InsertObject(tzuser, 7, "InsertKazuo");
        }
        #endregion

        #region 通过存储过程更新数据
        public int UpdateDataByPro(string proname, SqlParameter[] objectparam)
        {
            return base.ExecuteStorePro(proname, objectparam);
        }
        #endregion
        ///// <summary>
        ///// 添加用户
        ///// </summary>
        ///// <param name="user"></param>
        ///// <returns></returns>
        //public int Inserttz_Admin(tz_Admin _admin)
        //{
        //    return base.InsertObject(_admin, 2, "Inserttz_Admin");
        //}

        //public int UpdateHr_Admin(tz_Admin _admin)
        //{
        //    return base.InsertObject(_admin, 3, "Updatetz_Admin");
        //}

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="Fid"></param>
        /// <returns></returns>
        public DataSet GetHr_Admin(string Fid)
        {
            return QueryObjectAllSql("select * from tz_Admin where Id="+Fid);
        }

        ///// <summary>
        ///// 查询用户信息
        ///// </summary>
        ///// <param name="_object"></param>
        ///// <param name="_pagesize"></param>
        ///// <param name="_pageindex"></param>
        ///// <param name="_total"></param>
        ///// <returns></returns>
        IList<object> ICommDAL.Query_Admin(object _object, int _pagesize, int _pageindex, out int _total)
        {
            int _count = 0;
            try
            {
                SqlParameter[] logpara = new SqlParameter[3];
                yd_admin fadmin = (yd_admin)_object;
                fadmin.PageIndex = (_pageindex);
                fadmin.PageSize = (_pagesize);
                FillModel(logpara, fadmin);
                using (SqlConnection connection = new SqlConnection(ConStr))
                {
                    IList<object> _h = new List<object>();
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        PrepareCommand(cmd, connection, "Querytz_admin", logpara);
                        cmd.Parameters.Add("@TotalRecord", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
                        using (SqlDataReader dr = cmd.ExecuteReader())
                        {
                            base.AddEntityValue(_h, dr, ".Entity.tz_Admin");
                        }
                        _count = (int)cmd.Parameters["@TotalRecord"].Value;//返回值
                        _total = _count;
                    }
                    return _h;
                }
            }
            catch (Exception e)
            {
                _total = _count;
                LocalLog(e.Message);
                return null;
            }
        }
        ///// <summary>
        ///// 查询用户信息
        ///// </summary>
        ///// <param name="_object"></param>
        ///// <param name="_pagesize"></param>
        ///// <param name="_pageindex"></param>
        ///// <param name="_total"></param>
        ///// <returns></returns>
        IList<object> ICommDAL.Query_Shop(object _object, int _pagesize, int _pageindex, out int _total)
        {
            int _count = 0;
            try
            {
                yd_shop fadmin = (yd_shop)_object;
                SqlParameter[] logpara = new SqlParameter[] { 
                new SqlParameter("@shopName",fadmin.shopName),
                new SqlParameter("@isRemen",fadmin.remensele),
                new SqlParameter("@PageSize",_pagesize),
                new SqlParameter("@PageIndex",_pageindex)
            };
                using (SqlConnection connection = new SqlConnection(ConStr))
                {
                    IList<object> _h = new List<object>();
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        PrepareCommand(cmd, connection, "Queryyd_shop", logpara);
                        cmd.Parameters.Add("@TotalRecord", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
                        using (SqlDataReader dr = cmd.ExecuteReader())
                        {
                            base.AddEntityValue(_h, dr, ".yd_shop");
                        }
                        _count = (int)cmd.Parameters["@TotalRecord"].Value;//返回值
                        _total = _count;
                    }
                    return _h;
                }
            }
            catch (Exception e)
            {
                _total = _count;
                LocalLog(e.Message);
                return null;
            }
        }
        IList<object> ICommDAL.Query_Kazuo(object _object, int _pagesize, int _pageindex, out int _total)
        {
            int _count = 0;
            try
            {
                yd_kazuo fadmin = (yd_kazuo)_object;
                SqlParameter[] logpara = new SqlParameter[] { 
                new SqlParameter("@kzName",fadmin.kzName),
                new SqlParameter("@shopId",fadmin.shopid),
                new SqlParameter("@PageSize",_pagesize),
                new SqlParameter("@PageIndex",_pageindex)
            };
                using (SqlConnection connection = new SqlConnection(ConStr))
                {
                    IList<object> _h = new List<object>();
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        PrepareCommand(cmd, connection, "Queryyd_kazuo", logpara);
                        cmd.Parameters.Add("@TotalRecord", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
                        using (SqlDataReader dr = cmd.ExecuteReader())
                        {
                            base.AddEntityValue(_h, dr, ".yd_kazuo");
                        }
                        _count = (int)cmd.Parameters["@TotalRecord"].Value;//返回值
                        _total = _count;
                    }
                    return _h;
                }
            }
            catch (Exception e)
            {
                _total = _count;
                LocalLog(e.Message);
                return null;
            }
        }
        IList<object> ICommDAL.QueryView_yd_order(object _object, int _pagesize, int _pageindex, out int _total)
        {
            int _count = 0;
            try
            {
                yd_order fadmin = (yd_order)_object;
                SqlParameter[] logpara = new SqlParameter[] { 
                new SqlParameter("@username",fadmin.userName),
                new SqlParameter("@shopname",fadmin.shopName),
                new SqlParameter("@kzname",fadmin.kzName),
                new SqlParameter("@orderno",fadmin.orderno),
                new SqlParameter("@orderStatus",fadmin.orderStatus),
                new SqlParameter("@date1",fadmin.date1),
                new SqlParameter("@date2",fadmin.date2),
                new SqlParameter("@PageSize",_pagesize),
                new SqlParameter("@PageIndex",_pageindex)
            };
                using (SqlConnection connection = new SqlConnection(ConStr))
                {
                    IList<object> _h = new List<object>();
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        PrepareCommand(cmd, connection, "Query_yd_order_admin", logpara);
                        cmd.Parameters.Add("@TotalRecord", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
                        using (SqlDataReader dr = cmd.ExecuteReader())
                        {
                            base.AddEntityValue(_h, dr, ".yd_order");
                        }
                        _count = (int)cmd.Parameters["@TotalRecord"].Value;//返回值
                        _total = _count;
                    }
                    return _h;
                }
            }
            catch (Exception e)
            {
                _total = _count;
                LocalLog(e.Message);
                return null;
            }
        }
        IList<object> ICommDAL.Query_Action(object _object, int _pagesize, int _pageindex, out int _total)
        {
            int _count = 0;
            try
            {
                yd_action fadmin = (yd_action)_object;
                SqlParameter[] logpara = new SqlParameter[] { 
                new SqlParameter("@actionName",fadmin.actionName),
                new SqlParameter("@actionType",fadmin.actionType),
                new SqlParameter("@PageSize",_pagesize),
                new SqlParameter("@PageIndex",_pageindex)
            };
                using (SqlConnection connection = new SqlConnection(ConStr))
                {
                    IList<object> _h = new List<object>();
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        PrepareCommand(cmd, connection, "Queryyd_action", logpara);
                        cmd.Parameters.Add("@TotalRecord", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
                        using (SqlDataReader dr = cmd.ExecuteReader())
                        {
                            base.AddEntityValue(_h, dr, ".yd_action");
                        }
                        _count = (int)cmd.Parameters["@TotalRecord"].Value;//返回值
                        _total = _count;
                    }
                    return _h;
                }
            }
            catch (Exception e)
            {
                _total = _count;
                LocalLog(e.Message);
                return null;
            }
        }

        IList<object> ICommDAL.Query_Feedback(object _object, int _pagesize, int _pageindex, out int _total)
        {
            int _count = 0;
            try
            {
                yd_feedback fadmin = (yd_feedback)_object;
                SqlParameter[] logpara = new SqlParameter[] { 
                new SqlParameter("@userName",fadmin.userName),
                new SqlParameter("@PageSize",_pagesize),
                new SqlParameter("@PageIndex",_pageindex)
            };
                using (SqlConnection connection = new SqlConnection(ConStr))
                {
                    IList<object> _h = new List<object>();
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        PrepareCommand(cmd, connection, "Queryyd_feedback", logpara);
                        cmd.Parameters.Add("@TotalRecord", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
                        using (SqlDataReader dr = cmd.ExecuteReader())
                        {
                            base.AddEntityValue(_h, dr, ".yd_feedback");
                        }
                        _count = (int)cmd.Parameters["@TotalRecord"].Value;//返回值
                        _total = _count;
                    }
                    return _h;
                }
            }
            catch (Exception e)
            {
                _total = _count;
                LocalLog(e.Message);
                return null;
            }
        }
        IList<object> IBase.Query(object _object, int _pagesize, int _pageindex, out int _total)
        {
            throw new System.NotImplementedException();
        }

        IList<object> ICommDAL.Querytz_user(object _object, int _pagesize, int _pageindex, out int _total)
        {
            int _count = 0;
            try
            {
                yd_users fadmin = (yd_users)_object;
                SqlParameter[] logpara = new SqlParameter[] { 
                new SqlParameter("@userName",fadmin.userName),
                new SqlParameter("@PageSize",_pagesize),
                new SqlParameter("@PageIndex",_pageindex)
            };
                using (SqlConnection connection = new SqlConnection(ConStr))
                {
                    IList<object> _h = new List<object>();
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        PrepareCommand(cmd, connection, "Queryyd_user", logpara);
                        cmd.Parameters.Add("@TotalRecord", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
                        using (SqlDataReader dr = cmd.ExecuteReader())
                        {
                            base.AddEntityValue(_h, dr, ".yd_users");
                        }
                        _count = (int)cmd.Parameters["@TotalRecord"].Value;//返回值
                        _total = _count;
                    }
                    return _h;
                }
            }
            catch (Exception e)
            {
                _total = _count;
                LocalLog(e.Message);
                return null;
            }
        }


        public DataSet QueryView_yd_order(SqlParameter[] logpara, int _pagesize, int _pageindex, out int _total)
        {
            int _count = 0;
            try
            {
                using (SqlConnection connection = new SqlConnection(ConStr))
                {
                    //IList<object> _h = new List<object>();
                    DataSet ds = new DataSet();
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        PrepareCommand(cmd, connection, "Queryyd_order", logpara);
                        cmd.Parameters.Add("@TotalRecord", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
                        //using (SqlDataReader dr = cmd.ExecuteReader())
                        //{
                        //    base.AddEntityValue(_h, dr, ".Yd.Model.yd_order");
                        //}
                        
                        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                        {
                            da.Fill(ds);
                        }
                        _count = (int)cmd.Parameters["@TotalRecord"].Value;//返回值
                        _total = _count;
                    }
                    return ds;
                }
            }
            catch (Exception e)
            {
                _total = _count;
                LocalLog(e.Message);
                return null;
            }
        }
    }
}
